const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const moment = require('../../../lib/moment/moment.min.js');
var app = getApp();
var page
var that;
Page({
  data: {
    billsForMonth: [],
    bigMonthTxt: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
  },
  onLoad: function (options) {
    that = this;
    page = util.page.clone();
    that.toDoBill();
  },
  onPullDownRefresh() {
    page.reset();
    that.setData({
      billsForMonth: []
    });
    that.toDoBill();
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    that.toDoBill();
  },
  toDoBill: function () {
    app.loadPageExt(api.BillLs, page).then(rsp => {
      rsp.forEach(function (item, i) {
        item.cycle = moment(item.cycle).format('YYYY-M-DD');
        item.createTimeTxt = moment(item.createTime).format('YYYY年MM月DD日');
        item.bigMonthTxt = that.data.bigMonthTxt[Number(moment(item.createTime).format('M'))-1];
        item.totalCost = (item.totalCost/100).toFixed(2);
        item.rentStatusDesc = (item.rentStatus == "unpaid")?'未支付':'已支付';
        let tmp = {};
        tmp.date = moment(item.createTime).format('YYYY年MM月');
        var itemArr = [item];
        tmp.bills = itemArr;
        that.data.billsForMonth.push(tmp);
      });
      that.setData({ billsForMonth: that.data.billsForMonth });
    }).catch((err) => {
      console.log(err);
    });
  },
  toDetail(e){
    var id = e.currentTarget.id;
    var isx = e.currentTarget.dataset.isx;
    var bill = that.data.billsForMonth[id].bills[isx];
    wx.navigateTo({
      url: '/pages/bill/detail/detail?bill=' + JSON.stringify(bill),
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})
